package openjdk.stream;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Map to List with Lambda Expression
 */
public class Map2List {

//    Map<String,Person> map = new HashMap<String, Person>();
//    // To convert Map to List with lambda expression using CollectorsDemo.toList() is as follows.
//    List<String> valueList = map.values().stream().collect(CollectorsDemo.toList());
//
//    // If we want to sort the values before putting into List, we do it as follows.
//    List<String> sortedValueList = map.values().stream().sorted().collect(CollectorsDemo.toList());
//    // We can also convert Map to List of user object with given Comparator using Comparator.comparing().
//    List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
//            .map(e -> new Person(e.getKey(), e.getValue())).collect(CollectorsDemo.toList());
//    // Here Person is a user class. We can also use Map.Entry to get Map value and key as follows.
//    List<Person> list1 = map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))
//            .map(e -> new Person(e.getKey(), e.getValue())).collect(CollectorsDemo.toList());
//    // For comparison, we can also use Map.Entry.comparingByValue() and Map.Entry.comparingByKey()
//    // to sort the data on the basis of value and key respectively.
//    List<Person> list2 = map.entrySet().stream().sorted(Map.Entry.comparingByKey())
//            .map(e -> new Person(e.getKey(), e.getValue())).collect(CollectorsDemo.toList());

    // values();

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();
        map.put(23, "Mahesh");
        map.put(10, "Suresh");
        map.put(26, "Dinesh");
        map.put(11, "Kamlesh");
        System.out.println("--Convert Map Values to List--");
        List<String> valueList = map.values().stream().collect(Collectors.toList());
        valueList.forEach(n -> System.out.println(n));

        System.out.println("--Convert Map Values to List using sort--");
        List<String> sortedValueList = map.values().stream()
                .sorted().collect(Collectors.toList());
        sortedValueList.forEach(n -> System.out.println(n));

        System.out.println("--Convert Map keys to List--");
        List<Integer> keyList = map.keySet().stream().collect(Collectors.toList());
        keyList.forEach(n -> System.out.println(n));

        System.out.println("--Convert Map keys to List using sort--");
        List<Integer> sortedKeyList = map.keySet().stream()
                .sorted().collect(Collectors.toList());
        sortedKeyList.forEach(n -> System.out.println(n));


        System.out.println(">>>>>>>>>>>>>>>>>>> 华丽分隔线 >>>>>>>>>>>>>>>>>>>>>>>");

        List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
                .map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());

        list.forEach(l -> System.out.println("Id: " + l.getId() + ", Name: " + l.getName()));
    }


    static class Person {
        private Integer id;
        private String name;

        public Person(Integer id, String name) {
            this.id = id;
            this.name = name;
        }

        public Integer getId() {
            return id;
        }

        public String getName() {
            return name;
        }
    }

}
